package com.cmbchina.service;

import com.cmbchina.entity.Currency;
import com.cmbchina.entity.ExchangeRate;
import com.cmbchina.entity.SohuExchangeRate;

import java.math.BigDecimal;
import java.util.Objects;

public class SohuExchangeRateService extends SohuService implements IExchangeRateService {

    public ExchangeRate getSohuRate(Currency from, Currency to) {
        SohuExchangeRate sinaRate = getSohuExchangeRate(from, to);
        ExchangeRate rate = convertExhangeRate(sinaRate);
        return rate;
    }

    /**
     * 搜狐汇率格式转
     * @param sohuRate
     * @return
     */
    private ExchangeRate convertExhangeRate(SohuExchangeRate sohuRate){
        if(Objects.isNull(sohuRate)){
            throw new IllegalArgumentException("汇率不能为null");
        }

        //数据转换
        Currency sourceCurrency = sohuRate.getFromCurrency();
        Currency targetCurrency = sohuRate.getToCurrency();
        BigDecimal rate = sohuRate.getRate();
        return new ExchangeRate(rate, sourceCurrency, targetCurrency);
    }

}
